home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / sprites / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-01  |  9.6 KB  |  421 lines

  1. /*
  2.     init.c
  3.  
  4.     The initialization and termination routines
  5.  
  6. */
  7.  
  8. #include "global.h"
  9.  
  10. //
  11. // Initialise the first instance
  12. //
  13.  
  14. BOOL InitFirstInstance(HANDLE hInstance)
  15. {
  16.     WNDCLASS wc;
  17.  
  18.     
  19.     //
  20.     // define the class of window we want to register
  21.     //
  22.  
  23.     wc.lpszClassName    = szAppName;
  24.     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  25.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  26.     wc.hIcon            = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APP));
  27.     wc.lpszMenuName     = "Menu";
  28.     wc.hbrBackground    = GetStockObject(WHITE_BRUSH);
  29.     wc.hInstance        = hInstance;
  30.     wc.lpfnWndProc      = MainWndProc;
  31.     wc.cbClsExtra       = 0;
  32.     wc.cbWndExtra       = 0;
  33.     
  34.     if (!RegisterClass(&wc)) {
  35.         return FALSE;
  36.     }
  37.  
  38. #ifdef DEBUG
  39.  
  40.     wc.lpszClassName    = "Debug";
  41.     wc.style            = CS_HREDRAW | CS_VREDRAW;
  42.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  43.     wc.hIcon            = LoadIcon(hInstance,"Icon");
  44.     wc.lpszMenuName     = NULL;
  45.     wc.hbrBackground    = GetStockObject(WHITE_BRUSH);
  46.     wc.hInstance        = hInstance;
  47.     wc.lpfnWndProc      = DebugWndProc;
  48.     wc.cbClsExtra       = 0;
  49.     wc.cbWndExtra       = 0;
  50.     
  51.     return RegisterClass(&wc);
  52.  
  53. #endif // DEBUG
  54.  
  55. }
  56.  
  57. //
  58. // Init the current instance
  59. //
  60.  
  61. BOOL InitCurrentInstance(HANDLE hInstance, LPSTR lpszCmdLine, int cmdShow)
  62. {
  63.     HDC hDC;
  64.  
  65.     //
  66.     // Get any system metrics we use a lot
  67.     //
  68.  
  69.     hDC = GetDC(NULL);  // get a DC
  70.     GetTextMetrics(hDC, &tmSysFont);
  71.     ReleaseDC(NULL, hDC);
  72.  
  73.     //
  74.     // Create any GDI objects we use a lot
  75.     //
  76.  
  77.  
  78.  
  79.  
  80.  
  81.     //
  82.     // Create the main window
  83.     //
  84.  
  85.     hwndMain = CreateWindow(szAppName,
  86.                             szAppName,
  87.                             WS_OVERLAPPEDWINDOW,
  88.                             0,
  89.                             0,
  90.                             GetSystemMetrics(SM_CXSCREEN),
  91.                             GetSystemMetrics(SM_CYSCREEN) * 3 / 4,
  92.                             (HWND)NULL,
  93.                             (HMENU)NULL,
  94.                             hInstance,
  95.                             (LPSTR)NULL
  96.                             );
  97.     
  98.     if (!hwndMain) {
  99.         return FALSE;
  100.     }
  101.  
  102.     ShowWindow(hwndMain, cmdShow);
  103.     UpdateWindow(hwndMain);
  104.  
  105.     //
  106.     // Load the accelerator table
  107.     //
  108.  
  109.     hAccTable = LoadAccelerators(hInstance, "AccTable");
  110.     if (!hAccTable) {
  111.         return FALSE;
  112.     }
  113.  
  114.     //
  115.     // Register our private clipboard format
  116.     //
  117.  
  118.     uiCfSprite = RegisterClipboardFormat(SZ_SPRITE);
  119.  
  120. #ifdef DEBUG
  121.  
  122.     //
  123.     // Show what the screen device driver supports
  124.     //
  125.  
  126.     {
  127.  
  128.         HDC hDC;
  129.         WORD wCaps;
  130.  
  131.         hDC = GetDC(hwndMain);
  132.         wCaps = GetDeviceCaps(hDC, RASTERCAPS);
  133.         if (wCaps & RC_DI_BITMAP) {
  134.             dprintf3("Screen DC supports GetDIBits and SetDIBits");
  135.         } else {
  136.             dprintf3("Screen DC doesn't support GetDIBits or SetDIBits");
  137.         }
  138.         if (wCaps & RC_DIBTODEV) {
  139.             dprintf3("Screen DC supports SetDIBitsToDevice");
  140.         } else {
  141.             dprintf3("Screen DC doesn't support SetDIBitsToDevice");
  142.         }
  143.         if (wCaps & RC_STRETCHDIB) {
  144.             dprintf3("Screen DC supports StretchDIBits");
  145.         } else {
  146.             dprintf3("Screen DC doesn't support StretchDIBits");
  147.         }
  148.         wCaps = GetDeviceCaps(hDC, CAPS1);
  149.         if (wCaps & C1_TRANSPARENT) {
  150.             dprintf3("Screen DC supports transparency");
  151.         } else {
  152.             dprintf3("Screen DC doesn't support transparency");
  153.         }
  154.  
  155.  
  156.         ReleaseDC(hwndMain, hDC);
  157.     }
  158.  
  159. #endif // DEBUG
  160.  
  161.     //
  162.     // If a command line arg was supplied then use that as
  163.     // the INI file
  164.     //
  165.  
  166.     if (lstrlen(lpszCmdLine)) {
  167.  
  168.         lstrcpy(szIniFile, lpszCmdLine);
  169.         LoadScene(szIniFile);
  170.  
  171.     }
  172.  
  173. #ifdef DEBUG
  174.  
  175.     //
  176.     // Set up the debug menu state
  177.     //
  178.  
  179.     SetDebugLevel(__iDebugLevel);   // set menu state
  180.  
  181. #endif
  182.  
  183.     dprintf2("started");
  184.  
  185. #ifdef TIMER_TICKS
  186.     //
  187.     // Start the update timer
  188.     //
  189.  
  190.     uiTimer = SetTimer(hwndMain, 1, 500, NULL);
  191. #endif
  192.  
  193.     return TRUE;
  194. }
  195.  
  196. //
  197. // Close down and tidy up
  198. //
  199.  
  200. void Terminate()
  201. {
  202.     char buf[256];
  203.  
  204. #ifdef TIMER_TICKS
  205.     //
  206.     // Stop the update timer
  207.     //
  208.  
  209.     if (uiTimer) KillTimer(hwndMain, uiTimer);
  210. #endif
  211.  
  212.     //
  213.     // Delete the DIB driver DC
  214.     //
  215.  
  216.     if (hdcOffScreen) {
  217.         DeleteDC(hdcOffScreen);
  218.         hdcOffScreen = NULL;
  219.     }
  220.  
  221.     if (pdibOffScreen) {
  222.         DeleteDIB(pdibOffScreen);
  223.         pdibOffScreen = NULL;
  224.     }
  225.  
  226.     //
  227.     // Delete the background DIB
  228.     //
  229.  
  230.     if (pdibBkGnd) {
  231.         DeleteDIB(pdibBkGnd);
  232.         pdibBkGnd = NULL;
  233.     }
  234.  
  235.     //
  236.     // Delete any sprites
  237.     //
  238.  
  239.     DeleteSpriteList();
  240.  
  241.     //
  242.     // Delete the palette
  243.     //
  244.  
  245.     if (hpalCurrent) DeleteObject(hpalCurrent);
  246.  
  247.  
  248. #ifdef DEBUG
  249.  
  250.     //
  251.     // Save the debug level
  252.     //
  253.  
  254.     wsprintf(buf, "%d", __iDebugLevel);
  255.     WriteProfileString(szAppName, "debug", buf);
  256.     dprintf2("ended");
  257.  
  258. #endif // DEBUG
  259.         
  260. }
  261.  
  262. //
  263. // Read the INI file to set the initial state
  264. //
  265.  
  266. void LoadScene(LPSTR pFileName)
  267. {
  268.     char szFile[_MAX_PATH];
  269.     char szDIB[_MAX_PATH];
  270.     char szSprites[512];
  271.     LPSTR p;
  272.     PSPRITE pSprite;
  273.  
  274.     dprintf2("LoadScene(%s)", pFileName ? pFileName : (LPSTR)"(NULL)");
  275.  
  276.     if (!pFileName || !lstrlen(pFileName)) {
  277.  
  278.         //
  279.         // Show the file open dialog
  280.         //
  281.  
  282.         if (!PromptForFileName(hwndMain, 
  283.                                hAppInstance, 
  284.                                szFile,
  285.                                sizeof(szFile), 
  286.                                "Open Scene", 
  287.                                szIniFilter,
  288.                                "ini",
  289.                                PFFN_OPENFILE | PFFN_UPPERCASE)) {
  290.             return;
  291.         }
  292.  
  293.     } else {
  294.  
  295.         lstrcpy(szFile, pFileName);
  296.  
  297.     }
  298.  
  299.     //
  300.     // Get the name of the background dib
  301.     //
  302.  
  303.     dprintf3(" Getting bkgnd dib");
  304.     GetPrivateProfileString("Background",
  305.                             "dib",
  306.                             "",
  307.                             szDIB,
  308.                             sizeof(szDIB),
  309.                             szFile);
  310.  
  311.     if (lstrlen(szDIB) > 0) {
  312.         LoadBackground(szDIB, NO_UPDATE);
  313.     }
  314.  
  315.     //
  316.     // Get the list of sprites
  317.     //
  318.  
  319.     dprintf3(" Getting sprite list");
  320.     GetPrivateProfileString("Sprites",
  321.                             NULL,
  322.                             "",
  323.                             szSprites,
  324.                             sizeof(szSprites),
  325.                             szFile);
  326.  
  327.  
  328.     //
  329.     // Read each sprite entry and create the sprite
  330.     //
  331.  
  332.     p = szSprites;
  333.     while (*p) {
  334.  
  335.         //
  336.         // Load the sprite
  337.         //
  338.  
  339.         dprintf3("  Sprite: %s", p);
  340.         GetPrivateProfileString(p,
  341.                             "dib",
  342.                             "",
  343.                             szDIB,
  344.                             sizeof(szDIB),
  345.                             szFile);
  346.         if (!lstrlen(szDIB)) {
  347.  
  348.             dprintf1("No DIB= entry for sprite");
  349.  
  350.         } else {
  351.  
  352.             pSprite = LoadSprite(szDIB, NO_UPDATE);
  353.             if (pSprite) {
  354.  
  355.                 //
  356.                 // get any other info
  357.                 //
  358.  
  359.                 SetSpritePosition(pSprite,
  360.                                   GetPrivateProfileInt(p,
  361.                                                        "x",
  362.                                                        pSprite->x,
  363.                                                        szFile),
  364.                                   GetPrivateProfileInt(p,
  365.                                                        "y",
  366.                                                        pSprite->y,
  367.                                                        szFile),
  368.                                   NO_UPDATE);
  369.  
  370.                 SetSpriteZOrder(pSprite,
  371.                                 GetPrivateProfileInt(p,
  372.                                                      "z",
  373.                                                      pSprite->z,
  374.                                                      szFile),
  375.                                 NO_UPDATE);
  376.  
  377.  
  378.                 pSprite->vx = GetPrivateProfileInt(p,
  379.                                                   "vx",
  380.                                                   pSprite->vx,
  381.                                                   szFile);
  382.  
  383.  
  384.                 pSprite->vy = GetPrivateProfileInt(p,
  385.                                                   "vy",
  386.                                                   pSprite->vy,
  387.                                                   szFile);
  388.  
  389.                 pSprite->bSelectable = GetPrivateProfileInt(p,
  390.                                                   "selectable",
  391.                                                   pSprite->bSelectable,
  392.                                                   szFile);
  393.  
  394.                 dprintf3("  Sprite: %s, x:%u, y:%u, z:%u, vx:%u, vy:%u",
  395.                          (LPSTR)szDIB,
  396.                          pSprite->x,
  397.                          pSprite->y,
  398.                          pSprite->z,
  399.                          pSprite->vx,
  400.                          pSprite->vy);
  401.  
  402.             }
  403.         }
  404.  
  405.         p += lstrlen(p) + 1;
  406.  
  407.     }
  408.  
  409.     //
  410.     // Redraw the lot
  411.     //
  412.  
  413.     Redraw(NULL, UPDATE_SCREEN);
  414.  
  415.     //
  416.     // Start the update mechanism
  417.     //
  418.  
  419.     bAutoUpdate = TRUE;
  420. }
  421.